<template>
  <div class="table-box">
    <!-- <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" plain @click="onExport">导出</el-button>
      </div>
    </div>-->
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="游戏id">
          <el-input v-model="form.game_id" placeholder="游戏ID" />
        </el-form-item>
        <el-form-item label="游戏名字">
          <el-input v-model="form.name" placeholder="游戏名字" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="游戏id" align="center" />
          <el-table-column prop="name" label="英文名字" align="center" />
          <el-table-column prop="name_zh" label="中文名字" align="center" />
          <el-table-column prop="name_id" label="印尼名字" align="center" />
          <el-table-column prop="name_th" label="泰语名字" align="center" />
          <el-table-column prop="game_id" label="三方游戏id" align="center" />
          <el-table-column prop="is_hide" label="是否隐藏" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_hide ? 'success' : 'danger'">{{
                scope.row.is_hide ? "已隐藏" : "未隐藏"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="category_id" label="游戏类型" align="center" />
          <el-table-column prop="rank" label="排序" align="center" />
          <el-table-column label="默认icon" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.icon_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.icon_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="中文icon" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.icon_zh_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.icon_zh_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="印尼icon" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.icon_id_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.icon_id_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="泰语icon" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.icon_th_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.icon_th_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="推荐英文图" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.recommend_icon_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.recommend_icon_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="推荐中文图" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.recommend_icon_zh_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.recommend_icon_zh_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="推荐印尼图" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.recommend_icon_id_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.recommend_icon_id_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="推荐泰语图" align="center">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.recommend_icon_th_show"
                alt
                fit="fill"
                :preview-src-list="[scope.row.recommend_icon_th_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column
            prop="game_supplier"
            label="游戏厂商"
            align="center"
          />
          <el-table-column
            prop="game_vendor"
            label="游戏提供商"
            align="center"
          />
          <el-table-column
            prop="updatetime"
            label="更新时间"
            align="center"
            :formatter="formatterTime"
            width="180px"
          />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="180px"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                type="danger"
                @click="handleOperate(scope.$index, scope.row, 'delete')"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleUploaderImgRequest="handleUploader"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :supplierList="supplierList"
      :gameCategoryList="gameCategoryList"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime } from "@/utils/utils";
import {
  gameListApi,
  gameDelApi,
  gameAddApi,
  gameUpdateApi,
  gameSupplierListApi,
  gameCategoryApi,
} from "@/api/slotsgame/game/game";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const supplierList = ref([]); //游戏商
const gameCategoryList = ref([]); //游戏分类
const formData = {
  game_id: "",
  name: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  id: "",
  name: "",
  name_zh: "",
  name_id: "",
  name_th: "",
  game_id: "",
  icon: "",
  icon_show: "",
  icon_zh: "",
  icon_zh_show: "",
  icon_id: "",
  icon_id_show: "",
  icon_th: "",
  icon_th_show: "",
  recommend_icon: "",
  recommend_icon_show: "",
  recommend_icon_zh: "",
  recommend_icon_zh_show: "",
  recommend_icon_id: "",
  recommend_icon_id_show: "",
  recommend_icon_th: "",
  recommend_icon_th_show: "",
  src: "",
  game_supplier: "",
  app_mark: "",
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    game_id: form.game_id,
    name: form.name,
  };
};
// 获取游戏商
const getSupplierList = async () => {
  try {
    const res = await gameSupplierListApi({});

    supplierList.value = res.data;
  } catch (error) {
    supplierList.value = [];
  }
};
// 获取游戏分类
const getGameCategoryList = async () => {
  try {
    const res = await gameCategoryApi({ page: 1, pagesize: 200 });
    gameCategoryList.value = res.data.results;
  } catch (error) {
    gameCategoryList.value = [];
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await gameListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 新增
const onAdd = async () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  delete param.src;
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;

      const res = await gameAddApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      // console.log(param);
      const res = await gameUpdateApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    Object.assign(dialogForm, { ...row });
    dialogVisible.value = true;
  }
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此游戏?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await gameDelApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};
// //上传图标
const handleUploader = (res, name, src) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });

    dialogForm[src] = res.data.pic_url || "";
    dialogForm[name] = res.data.name || "";
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  getSupplierList();
  getGameCategoryList();
});
</script>
